package com.example.wm.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.wm.OV.OfficailOv;
import com.example.wm.common.Result;
import com.example.wm.common.ResultData;
import com.example.wm.entity.Custonmer;
import com.example.wm.entity.OfficialBusiness;
import com.example.wm.entity.OfficialBusinessDetial;
import com.example.wm.mapper.OfficialBusinessMapper;
import com.example.wm.service.OfficialBusinessDetialService;
import com.example.wm.service.OfficialBusinessService;
import com.example.wm.utils.TsyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/OfficialBusinessController")
@CrossOrigin
//出差
public class OfficialBusinessController {
    @Resource
    private OfficialBusinessService officialBusinessService;
    @Resource
    private OfficialBusinessMapper officialBusinessMapper;
    @Resource
    private OfficialBusinessDetialService officialBusinessDetialService;
    @Resource
    private TsyUtils tsyUtils;
    @Autowired
    private RedisTemplate redisTemplate;
    @Transactional
    @PostMapping("/save")
    public Result save(@RequestBody OfficialBusiness officialBusiness) {
        officialBusiness.setSqtime(tsyUtils.getnowtime());
        officialBusinessService.save(officialBusiness);

      Set<String> set=redisTemplate.keys("officialBusiness"+officialBusiness.getCcperson()+"*");
      redisTemplate.delete(set);

        return ResultData.success(officialBusiness.getId());

    }
    @Transactional
    @PostMapping("/statussubmit")
    public Result statussubmit(@RequestBody OfficialBusiness officialBusiness) {
//
//        lq.ne(OfficialBusinessDetial::getDktime,null);
//       long n=   officialBusinessDetialService.count(lq);
//       if(n==0){
        officialBusinessService.updateById(officialBusiness);
        LambdaQueryWrapper<OfficialBusiness> lq=new LambdaQueryWrapper<>();
        lq.eq(OfficialBusiness::getId,officialBusiness.getId());
        OfficialBusiness ofb=     officialBusinessService.getOne(lq);
//        OfficailOv of=new OfficailOv();
//        of.setOfficialBusiness(ofb);
        Set ss=   redisTemplate.keys("officialBusiness"+officialBusiness.getCcperson()+"*");

//        Boolean bo=redisTemplate.hasKey("officialBusiness"+officialBusiness.getCcperson()+officialBusiness.getId());
        if(!ss.isEmpty()){
         redisTemplate.delete(ss);
         //   redisTemplate.delete("officialBusiness"+officialBusiness.getCcperson());
//            redisTemplate.opsForValue().set("officialBusiness"+officialBusiness.getCcperson()+officialBusiness.getId(),of);

        }
         return ResultData.success("成功");
//       }

//        return ResultData.error(1000,"未完成全部打卡");

    }
    @Transactional
    @PostMapping("/savelist")
    public Result savelist(@RequestBody OfficialBusinessDetial officialBusinessDetial) {
        System.out.println(officialBusinessDetial);
        officialBusinessDetial.setAddtime(tsyUtils.getnowtime());
        Boolean b=   officialBusinessDetialService.save(officialBusinessDetial);
        return ResultData.success(b);

    }

    @PostMapping("/transterurl")
//    配合elemenui-plus 上传图片
    public Result<String> transterurlReimbursementList(@RequestParam("file") MultipartFile file) {
        byte[] ss=null;
        String base64EncodedString=null;
        try{
            ss = file.getBytes();
            base64EncodedString = Base64.getEncoder().encodeToString(ss);
        }catch(IOException e){
            e.printStackTrace();
        }
        return  ResultData.success("操作成功",base64EncodedString);
    }
    @Transactional
    @PostMapping("/uploadimg")
    public Result uploadimgs(@RequestBody JSONObject jsonObject)  {
        OfficialBusiness o=new OfficialBusiness();
        o.setId(jsonObject.getInteger("id"));
        o.setBeginimg(jsonObject.getBytes("beginimg"));
        o.setBegintime(tsyUtils.getnowtime());
        o.setBeginlnglog(jsonObject.getString("latlng"));
        officialBusinessMapper.updateById(o);
        LambdaQueryWrapper<OfficialBusiness> lq=new LambdaQueryWrapper<>();
        lq.eq(OfficialBusiness::getId,jsonObject.getInteger("id"));
        OfficialBusiness ofb= officialBusinessService.getOne(lq);
        System.out.println(o);
//        OfficailOv of=new OfficailOv();
//        of.setOfficialBusiness(ofb);
        Set ss=   redisTemplate.keys("officialBusiness"+jsonObject.getString("name")+"*");

//        Boolean bo=redisTemplate.hasKey("officialBusiness"+jsonObject.getString("name")+jsonObject.getInteger("id"));
        if(!ss.isEmpty()){
            redisTemplate.delete(ss);
//            redisTemplate.delete("officialBusiness"+jsonObject.getString("name")+jsonObject.getInteger("id"));
//            redisTemplate.opsForValue().set("officialBusiness"+jsonObject.getString("name")+jsonObject.getInteger("id"),of);

        }

        return  ResultData.success("操作成功");
    }
    @Transactional
    @CrossOrigin("*")
    @PostMapping("/uploadimgend")
    public Result uploadimgsend(@RequestBody JSONObject jsonObject)  {
        OfficialBusiness o=new OfficialBusiness();
        o.setId(jsonObject.getInteger("id"));
        o.setEndimg(jsonObject.getBytes("endimg"));
        o.setFinishtime(tsyUtils.getnowtime());
        o.setEndlnglog(jsonObject.getString("latlng"));
        int b=   officialBusinessMapper.updateById(o);
        OfficailOv of=new OfficailOv();
        of.setOfficialBusiness(o);
        Boolean bo=redisTemplate.hasKey("officialBusiness"+jsonObject.getString("name")+jsonObject.getInteger("id"));
        if(bo){
            redisTemplate.delete("officialBusiness"+jsonObject.getString("name")+jsonObject.getInteger("id"));
            redisTemplate.opsForValue().set("officialBusiness"+jsonObject.getString("name")+jsonObject.getInteger("id"),of);

        }
        System.out.println(o);

        return  ResultData.success(b);
    }
//    获取alllist
     @PostMapping("/getlistall")
      public Result getlistwsh(@RequestBody JSONObject jsonObject) {
        String name=jsonObject.getString("name");
         LambdaQueryWrapper<OfficialBusiness> lq=new LambdaQueryWrapper<>();
         lq.eq(OfficialBusiness::getCcperson,name);
         lq.orderByDesc(OfficialBusiness::getSqtime);
        List<OfficialBusiness> list=  officialBusinessService.list(lq);
         LambdaQueryWrapper<OfficialBusinessDetial> lq2=new LambdaQueryWrapper<>();
         ArrayList<OfficailOv> arrayList=new ArrayList<>();
         ArrayList<Object> arrayList2=new ArrayList<>();
         Set<String> s=  redisTemplate.keys("officialBusiness"+name+"*");
         System.out.println(s);
        if(!s.isEmpty()){
            for(String set:s){
            Object obs=    redisTemplate.opsForValue().get(set);
                arrayList2.add( obs);
            }
            return ResultData.success(arrayList2);
        }else{
            for (OfficialBusiness o:list){
                OfficailOv of=new OfficailOv();
                of.setOfficialBusiness(o);
                System.out.println(of);
                redisTemplate.opsForValue().set("officialBusiness"+name+o.getId(),of);

//             lq2.eq(OfficialBusinessDetial::getBusinessid,  o.getId());
//             List<OfficialBusinessDetial> list2=  officialBusinessDetialService.list(lq2);
//             of.setOfficialBusinessDetialList(list2);
                arrayList.add(of);

            }
            return  ResultData.success(arrayList);

        }



 }
// 根据id查询信息
@PostMapping("/getlistallbyid")

public Result getlistwshbyid(@RequestBody JSONObject jsonObject) {

    Integer id=jsonObject.getInteger("id");
    LambdaQueryWrapper<OfficialBusinessDetial> lq2=new LambdaQueryWrapper<>();
      lq2.eq(OfficialBusinessDetial::getBusinessid,id);
      List<OfficialBusinessDetial> o=officialBusinessDetialService.list(lq2);
          return  ResultData.success(o);
}
// 查询打卡条例
    @PostMapping("/getlistunfinished")
    public Result getlistunfinished(@RequestBody JSONObject jsonObject) {
        String name=jsonObject.getString("name");
        LambdaQueryWrapper<OfficialBusiness> lq=new LambdaQueryWrapper<>();
        lq.eq(OfficialBusiness::getCcperson,name);
        lq.orderByDesc(OfficialBusiness::getSqtime);
        lq.eq(OfficialBusiness::getSubmitstatus,0);
        lq.eq(OfficialBusiness::getStatus,jsonObject.getString("status"));
//        lq.eq(OfficialBusiness::getStatus,"已通过");
        List<OfficialBusiness> list=  officialBusinessService.list(lq);


        LambdaQueryWrapper<OfficialBusinessDetial> lq2=new LambdaQueryWrapper<>();
        ArrayList<OfficailOv> arrayList=new ArrayList<>();


        for (OfficialBusiness o:list){
            OfficailOv of=new OfficailOv();
            of.setOfficialBusiness(o);
//            String base64EncodedString = Base64.getEncoder().encodeToString(o.getBeginimg());
//            System.out.println("base64EncodedString:"+base64EncodedString);
//            String re="data:image/jpeg;base64,"+base64EncodedString;
//             o.setBegintime(re);
            lq2.eq(OfficialBusinessDetial::getBusinessid,  o.getId());
            List<OfficialBusinessDetial> list2=  officialBusinessDetialService.list(lq2);
            of.setOfficialBusinessDetialList(list2);
            arrayList.add(of);

        }


        return  ResultData.success(arrayList);
    }
//    待审核
    @PostMapping("/statusdsh")
    public Result getstatusdsh(@RequestBody JSONObject jsonObject){
        LambdaQueryWrapper<OfficialBusiness> lq=new LambdaQueryWrapper<>();
        lq.eq(OfficialBusiness::getStatus,"待审核");
        List<OfficialBusiness> list =officialBusinessService.list(lq);
        return ResultData.success(list);
    }



}
